//
//  TF_BackImagePreviewView.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2022/1/11.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_BackImagePreviewView: UIView,UIScrollViewDelegate,UIGestureRecognizerDelegate {
    var srcollView:UIScrollView!
    var imageView:UIImageView!

    override init(frame: CGRect) {
        super.init(frame: frame)
    }


    @objc func tapUiscrollView()  {
        self.removeFromSuperview()
    }
   @objc public  func layoutSubviewsUI(imageUrl:String) {
       if self.srcollView == nil {
           self.backgroundColor = UIColor.white
           self.srcollView = UIScrollView.init()
           //添加点按击手势监听器
           let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(tapUiscrollView))
           tapGesture.delegate = self
           tapGesture.numberOfTapsRequired = 1 //设置点按次数，默认为1，注意在iOS中很少用双击操作
           tapGesture.numberOfTouchesRequired = 1 //点按的手指数
   
           self.srcollView.addGestureRecognizer(tapGesture)
        
           self.srcollView.delegate = self;
           self.srcollView.isUserInteractionEnabled = true
           self.srcollView.showsHorizontalScrollIndicator = true //是否显示侧边的滚动栏

           self.srcollView.showsVerticalScrollIndicator = false

           self.srcollView.scrollsToTop = false

           self.srcollView.isScrollEnabled = true

           self.srcollView.frame = self.bounds;

           self.imageView = UIImageView.init()
           self.imageView.sd_setImage(with: NSURL.init(string: imageUrl) as URL?, completed: nil)
           self.imageView.contentMode = .scaleAspectFit
           //设置这个_imageView能被缩放的最大尺寸，这句话很重要，一定不能少,如果没有这句话，图片不能缩放
           self.imageView.frame = CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height)
       
           self.addSubview(self.srcollView)
           self.srcollView.addSubview(self.imageView)

         
           self.srcollView.minimumZoomScale = 0.25

           self.srcollView.maximumZoomScale = 3.0
           self.srcollView.setZoomScale(1, animated: false)
           
       }
    }
    
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.imageView
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
